<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-row :gutter="100">
        <el-col :span="6">
          <el-input
            v-model="listQuery.name"
            placeholder="请输入内容"
            clearable
            @keyup.enter.native="handleFilter"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click.native="handleFilter"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="medium"
            @click="handleAdd"
            >新增律所</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="dataList"
        border
        v-loading="listLoading"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="#"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          label="名称"
          prop="name"
        ></el-table-column>
        <el-table-column
          align="center"
          label="信用代码"
          prop="credit_code"
        ></el-table-column>
        <el-table-column
          align="center"
          label="律师详细地址"
          prop="address"
        ></el-table-column>
        <el-table-column
          align="center"
          label="法定代表人"
          prop="legal_person"
        ></el-table-column>
        <el-table-column
          align="center"
          label="联系电话"
          prop="telephone"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="300px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话框表单 -->
    <el-dialog :visible.sync="dialogVisible" :title="getTitle" width="40%">
      <el-form
        :model="form"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
        label-position="right"
        style="width: 95%; padding: 0 30px"
      >
        <el-form-item label="律所名称" prop="name">
          <el-input v-model="form.name" placeholder="律所名称" clearable />
        </el-form-item>
        <el-form-item label="信用代码">
          <el-input
            v-model="form.credit_code"
            placeholder="信用代码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            v-model="form.address"
            placeholder="详细地址"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="法定代表人">
          <el-input
            v-model="form.legal_person"
            placeholder="法定代表人"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="telephone">
          <el-input v-model="form.telephone" placeholder="手机号码" clearable />
        </el-form-item>
        <el-form-item label="律所所在市" prop="position">
          <el-input
            v-model="form.position"
            placeholder="律所所在市"
            clearable
          />
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
          <el-input
            v-model="form.bank"
            placeholder="开户行"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="银行账号" prop="account">
          <el-input
            v-model="form.account"
            placeholder="银行账号"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOffices, addOffice, updateOffice, delOffice } from "@/api/office";

const defaultForm = {
  id: null,
  name: "",
  credit_code: "",
  address: "",
  legeal_person: "",
  telephone: "",
  position: "",
  bank: "",
  account: "",
};

export default {
  name: "Office",
  computed: {
    getTitle() {
      return this.dialogType === "edit"
        ? `编辑${this.title}`
        : `新增${this.title}`;
    },
  },
  data() {
    // 验证手机号的规则
    const checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };

    return {
      form: Object.assign({}, defaultForm),
      formRules: {
        name: [{ required: true, message: "请输入律师名称", trigger: "blur" }],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        // telephone: [
        //   { required: true, message: "请输入手机号", trigger: "blur" },
        //   { validator: checkMobile, trigger: "blur" },
        // ],
      },

      title: "律所",
      dialogVisible: false,
      dialogType: "new",
      dataList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        name: null,
      },
    };
  },

  created() {
    this.getOffices();
  },
  methods: {
    // 员工列表
    getOffices() {
      this.listLoading = true;
      getOffices(this.listQuery)
        .then((res) => {
          this.total = res.count;
          this.dataList = res;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log(error.message);
          this.$message.error("获取律所数据失败");
          this.listLoading = false;
        });
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1;
      this.getOffices();
    },
    // 增加
    handleAdd() {
      this.form = Object.assign({}, defaultForm);

      this.dialogType = "new";
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
    //
    handleEdit(scope) {
      this.form = Object.assign({}, scope.row);
      this.dialogType = "edit";

      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
    //
    handleDelete({ $index, row }) {
      this.$confirm("确定要删除该信息吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delOffice(row.id);

          this.dataList.splice($index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          console.error(err);
          this.$message.error("删除失败!");
        });
    },
    // 保存操作
    async handleSave() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return;

        const isEdit = this.dialogType === "edit";
        if (isEdit) {
          const res = await updateOffice(this.form);
          console.log(isEdit, res);
          const index = this.dataList.findIndex((v) => v.id === res.id);
          this.dataList.splice(index, 1, res);
        } else {
          const res = await addOffice(this.form);
          const { id } = res;
          if (id > 0) {
            this.getOffices();
          }
        }

        const { name, address, telephone } = this.form;
        this.dialogVisible = false;
        this.$notify({
          title: "Success",
          dangerouslyUseHTMLString: true,
          message: `
              <div>姓名: ${name}</div>
              <div>手机号: ${address}</div>
              <div>身份证: ${telephone}</div>
            `,
          type: "success",
        });
      });
    },
  },
};
</script>

<style lang='scss' scoped>
.app-container {
  // .box-card{
  //   margin-top: 30px;
  // }
  // .el-table {
  //   margin-top: 10px;
  //   width: 100%;
  // }
  // .permission-tree {
  //   margin-bottom: 30px;
  // }
  // .el-select {
  //   width: 100%;
  // }
}
</style>